﻿@model IEnumerable<General.Entities.Category>
@{
    Layout = "_PopupLayout";
    ViewData["Title"] = "功能能菜单";
    var canInit = WorkContext.OwnPermission("initCategory");
}

@functions{
    public HtmlString RenderCategory(Category item)
    {
        var childNodes = Model.Where(o => o.FatherID == item.ResouceID).ToList();

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        sb.Append("<tr>");
        sb.Append("<td style=\"min-width:200px;\">");
        sb.Append("<i class=\"" + item.CssClass + "\"></i>");
        sb.Append(item.Name);
        if (item.IsMenu)
        {
            sb.Append(" <small class=\"text-primary\">菜单</small>");
        }
        sb.Append("</td>");

        if (childNodes.Any())
        {
            sb.Append("<td>");
            sb.Append("<table class=\"table table-striped table-bordered dataTable no - footer\">");
            sb.Append("<tbody>");

            foreach (var node in childNodes)
            {
                sb.Append(RenderCategory(node));
            }

            sb.Append("</tbody>");
            sb.Append("</table>");
            sb.Append("</td>");
        }
        else
        {
            sb.Append("<td></td>");
        }

        sb.Append("</tr>");
        return new HtmlString(sb.ToString());
    }
}

<div class="page-content">
    <div class="row">
        <div class="col-sm-12">
            @if (canInit)
            {
                <a asp-route="initCategory" class="btn btn-sm btn-primary" id="btnInitCategory"><i class="ace-icon fa fa-refresh"></i>初始化</a>
            }
        </div>
    </div>
    <div class="space-2"></div>
    <div class="row">
        <div class="col-sm-12">
            <table id="user_datatable" class="table table-striped table-bordered table-hover dataTable no-footer">
                <tbody>
                    @{
                        var nodes = Model.Where(o => String.IsNullOrEmpty(o.FatherID)).OrderBy(o => o.Sort).ToList();
                        foreach (var node in nodes)
                        {
                            @RenderCategory(node);
                        }
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>
<script>
    $(function () {
        $('#btnInitCategory').click(function (e) {
            e.preventDefault();
            var url = this.href;
            layer.confirm('您确定要初始化？', function () {
                layer.load();
                $.get(url, function (data) {
                    layer.closeAll();
                    layer.msg(data.Message, { icon: 1 });
                    window.location.reload();
                });
            });
        });

    })
</script>









